/*
 * monster.c

 *
 *  Created on: 4 avr. 2013
 *      Author: xavier
 */

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stddef.h>
#include <time.h>
#include "SDL.h"
#include "SDL_image.h"

#include "../include/game.h"
#include "../include/misc.h"
#include "../include/monster.h"
#include "../include/misc.h"
#include "../include/bomb.h"
#include "../include/liste.h"


int monster_number=0;

struct s_monster {
	int x, y;
	SDL_Surface * directed_img[4];
	Uint32 last_state;
	enum e_way current_way;
	t_monster next;
	t_monster prec;
	int numb;//To identify the monster in the list

	int validity;//if 0, the monster is not valid and not active

};

/*struct monster_list {
	struct monster_list * next;
	t_monster monster;

};*/

void monster_set_validity(t_monster monster,int x)
{
	monster->validity=x;
}

int monster_get_validity(t_monster monster)
{
	return monster->validity;
}




t_monster new_monster(int x,int y,t_monster monster)
{
	assert(monster);



	t_monster monster_new = malloc(sizeof(*monster));
	assert(monster_new);

			monster_new->directed_img[WEST] = load_image(IMG_MONSTER_LEFT);
			monster_new->directed_img[EAST] = load_image(IMG_MONSTER_RIGHT);
			monster_new->directed_img[NORTH] = load_image(IMG_MONSTER_UP);
			monster_new->directed_img[SOUTH] = load_image(IMG_MONSTER_DOWN);

	monster_new->current_way = SOUTH;

	monster_new->next=monster;
	monster_new->prec=NULL;
	monster->prec=monster_new;
	monster_new->x=x;
	monster_new->y=y;




	return monster_new;


}



//To delete all the monster in the case of map change
t_monster delete_all_monster(t_game game,t_map map,t_monster monster)
{
	while(monster->next!=NULL || monster->prec!=NULL)
	{
		if(monster->next!=NULL && monster->prec!=NULL)
				{
					t_monster monster_old=monster;
					map_set_cell_type(map,monster_get_x(monster),monster_get_y(monster),CELL_EMPTY);
					monster=monster->prec;
					monster->next=monster->next->next;

					free(monster_old);

					game_set_validity(game,1);

				}
				else if(monster->next!=NULL && monster->prec==NULL)
				{
					t_monster monster_old=monster;

					map_set_cell_type(map,monster_get_x(monster),monster_get_y(monster),CELL_EMPTY);
					monster=monster->next;
					free(monster_old);
					monster->prec=NULL;

					game_set_validity(game,1);
				}
				else if(monster->next==NULL && monster->prec!=NULL)
				{
					t_monster monster_old=monster;
					map_set_cell_type(map,monster_get_x(monster),monster_get_y(monster),CELL_EMPTY);
					monster=monster->prec;

					//free(monster->next);
					monster->next=NULL;
					free(monster_old);
					monster=monster_init_list(monster);
					game_set_validity(game,1);
				}

	}
	return monster;
}


int monster_get_number(t_monster monster)
{
	return monster->numb;
}

void monster_set_number(t_monster monster)
{
	monster->numb=monster_number;
	monster_number=monster_number+1;
}

t_monster delete_monster(t_game game,t_map map,int coorx,int coory,t_monster monster)
{
		assert(monster);
		int x;


		monster=monster_init_list(monster);
		while(monster->x!=coorx || monster->y!=coory )
			{
				if(monster->next==NULL && monster->x!=coorx && monster->y!=coory)
				{
					monster_init_list(monster);
					break;}
				else
					monster=monster->next;

			}
		/*On passe à la fonction les coordonnées du monstres à détruire et on parcourt la liste de monstres en comparant ces coordonnées
		 * à celles des différents monstres. Si on en trouve un qui a les bonnes coordonnées (ce qui est toujours le cas en théorie), on le
		 * supprime OU, si il s'agit du dernier de la liste (suivant et précédent nul), on le rend juste inactif.
		 *
		 */
		x=monster_get_number(monster);
		printf("le numero du monstre à détruire est %d",x);
		monster=monster_init_list(monster);
		while(monster->numb!=x)
		{
			if(monster->next==NULL && monster->numb!=x)
			{
				monster=monster_init_list(monster);
				return monster;
			}
			monster=monster->next;
		}
		if(monster->next!=NULL && monster->prec!=NULL)
		{
			t_monster monster_old=monster;
			map_set_cell_type(map,monster_get_x(monster),monster_get_y(monster),CELL_EMPTY);
			/*monster=monster->prec;


			monster->next=monster->next->next;*/
			monster->next->prec=monster->prec;
			monster->prec->next=monster->next;
			free(monster_old);

			game_set_validity(game,1);

		}
		else if(monster->next!=NULL && monster->prec==NULL)
		{
			t_monster monster_old=monster;
			map_set_cell_type(map,monster_get_x(monster),monster_get_y(monster),CELL_EMPTY);
			monster=monster->next;

			//free(monster->prec);
			monster->prec=NULL;
			free(monster_old);

			game_set_validity(game,1);
		}
		else if(monster->next==NULL && monster->prec!=NULL)
		{
			t_monster monster_old=monster;
			map_set_cell_type(map,monster_get_x(monster),monster_get_y(monster),CELL_EMPTY);
			monster=monster->prec;

			//free(monster->next);
			monster->next=NULL;
			free(monster_old);
			monster=monster_init_list(monster);
			game_set_validity(game,1);
		}
		else if(monster->next==NULL && monster->prec==NULL)
				{
				map_set_cell_type(map,monster_get_x(monster),monster_get_y(monster),CELL_EMPTY);
				game_set_validity(game,0);

				}



		x=-1;
		monster=monster_init_list(monster);
		return monster;

}

/*void cross_monster(t_monster monster)
{
	assert(monster);
	monster=monster->next;
}*/



t_monster monster_get_next(t_monster monster)
{
	return monster->next;

}


void set_last_state(t_monster monster)
{
	monster->last_state=SDL_GetTicks();
}

Uint32 get_last_state(t_monster monster)
{
	return monster->last_state;
}




t_monster monster_init_list(t_monster monster)
{
	while(monster->prec!=NULL)
	{
		monster=monster->prec;
	}
	return  monster;
}

//To create a new list in memory
t_monster monster_init_if_empty(t_map map,int x, int y)
{
	t_monster monster = malloc(sizeof(*monster));
		if (!monster)
			error("Memory error");



		monster->current_way = SOUTH;
		monster->x=0;
		monster->y=0;
		map_set_cell_type(map,x,y,CELL_EMPTY);

		//printf("%d",monster->numb);


		monster->next=NULL;
		monster->prec=NULL;



		return monster;

}



t_monster monster_init(int x,int y) {
	t_monster monster = malloc(sizeof(*monster));
	if (!monster)
		error("Memory error");

		monster->directed_img[WEST] = load_image(IMG_MONSTER_LEFT);
		monster->directed_img[EAST] = load_image(IMG_MONSTER_RIGHT);
		monster->directed_img[NORTH] = load_image(IMG_MONSTER_UP);
		monster->directed_img[SOUTH] = load_image(IMG_MONSTER_DOWN);



	monster->current_way = SOUTH;
	monster->x=x;
	monster->y=y;


	//printf("%d",monster->numb);


	monster->next=NULL;
	monster->prec=NULL;



	return monster;
}


void monster_free(t_monster monster) {
	assert(monster);
	int i;
	for (i = 0; i < 4; i++)
		SDL_FreeSurface(monster->directed_img[i]);

	free(monster);
}

int monster_get_x(t_monster monster) {
	assert(monster != NULL);
	return monster->x;
}

int monster_get_y(t_monster monster) {
	assert(monster != NULL);
	return monster->y;
}

void monster_set_current_way(t_monster monster, enum e_way way) {
	assert(monster);
	monster->current_way = way;
}


void monster_from_map(t_monster monster, t_map map) {
	int i, j;
	for (i = 0; i < map_get_width(map); i++) {
		for (j = 0; j < map_get_height(map); j++) {
			if (map_get_cell_type(map, i, j) == CELL_MONSTER) {
				monster->x = i;
				monster->y = j;
			}
		}
	}
}

static int monster_move_aux(t_monster monster, t_map map, int x1, int y1, int x2,
		int y2) {

	if (!map_is_inside(map, x1, y1))
		return 0;

	switch (map_get_cell_type(map, x1, y1)) {
	case CELL_SCENERY:
		return 0;
		break;

	case CELL_CASE:
		return 0;
		break;

	case CELL_GOAL:
			return 0;
			break;


	case CELL_BOMB:
		return 0;
		break;

	case CELL_DOOR:
			return 0;
			break;

	/*case CELL_CLOSED_DOOR:
			return 0;
			break;*/



	case CELL_MONSTER:
		return 0;
		break;

	/*case CELL_monster:
		break;

	case CELL_BOMB:
		return 0;
		break;*/

	default:
		break;
	}

	// monster has moved
	return 1;
}

int monster_move(t_monster monster, t_map map) {
	int x = monster->x;
	int y = monster->y;
	int move = 0;


	if(monster->validity!=0)
	{
	switch (monster->current_way) {
	case NORTH:
		if (monster_move_aux(monster, map, x, y - 1, x, y - 2) && monster->y>=1 ) {
			monster->y--;
			move = 1;
		}
		break;

	case SOUTH:
		if (monster_move_aux(monster, map, x, y + 1, x, y + 2) && monster->y<=10) {
			monster->y++;
			move = 1;
		}
		break;

	case WEST:
		if (monster_move_aux(monster, map, x - 1, y, x - 2, y) && monster->x>=1) {
			monster->x--;
			move = 1;
		}
		break;

	case EAST:
		if (monster_move_aux(monster, map, x + 1, y, x + 2, y) && monster->x<=10) {
			monster->x++;
			move = 1;
		}
		break;
	}

	if (move) {
		//map_set_cell_type(map, x, y, CELL_EMPTY);
		map_set_cell_type(map, monster->x, monster->y, CELL_MONSTER);
	}
	return move;
	}
	return 0;
}

void monster_display(t_monster monster, SDL_Surface *screen) {




	while(monster!=NULL)
	{


		if(monster->validity!=0){
	assert(monster);

	assert(monster->directed_img[monster->current_way]);
	assert(screen);

	SDL_Rect place;
	place.x = monster->x * SIZE_BLOC;
	place.y = monster->y * SIZE_BLOC;

	SDL_BlitSurface(monster->directed_img[monster->current_way], NULL, screen,
			&place);

	monster=monster->next;}
		else
	monster=monster->next;}
}









